const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        productName: '',
        price: '',
        files: [],
        filesId: [],
        enbleUpload: true,
        uploading: false,
        uploadFail: false,
        localFiles: [],
        enableUpload: true,
        isPercentSet: false,

    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '商品添加'
        })

    },
    bindProductName(e) {
        this.setData({
            productName: e.detail.value
        })
    },
    bindProductPrice(e) {
        this.setData({
            price: e.detail.value
        })
    },
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                that.setData({
                    localFiles: that.data.localFiles.concat(res.tempFilePaths),
                    uploading: true,
                    progress: 0,
                });

                const upload = wx.uploadFile({
                    // url: 'http://192.168.90.189:8080/vending-admin/active/uploadFile',
                    url: app.globalData.APIURL + '/vending-admin/active/uploadFile',
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    formData: {},
                    success: function (res) {
                        var data = JSON.parse(res.data);
                        console.log(data);
                        if (data.success == 1) {
                            that.setData({
                                files: that.data.files.concat(that.data.localFiles[that.data.localFiles.length - 1]),
                                filesId: that.data.filesId.concat(data.fileId),
                                uploading: false,

                            });
                            that.setData({
                                enableUpload: that.data.files.length < 1 ? true : false,
                            })


                        } else {
                            var msg = data.msg;
                            wx.showModal({
                                content: msg,
                                showCancel: false,
                                success: function (res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定')
                                    }
                                }
                            });
                        }
                        console.log(res);
                    },
                    fail: function (err) {
                        console.log(err);
                    }
                });
                upload.onProgressUpdate((res) => {
                    that.setData({
                        progress: res.progress
                    })

                })
            }
        })
    },
    previewImage: function (e) {
        wx.previewImage({
            current: e.currentTarget.id, // 当前显示图片的http链接
            urls: this.data.files // 需要预览的图片http链接列表
        })
    },
    addProduct: util.throttle(function () {
        if (this.data.filesId.length < 1) {
            wx.showModal({
                title: '请上传商品图片',
                showCancel: false,
                success: function (res) {
                }
            });
        } else {
            wx.showLoading({
                title: '加载中',
            });
            var that = this;
            wx.request({
                url: app.globalData.APIURL + '/vending-admin/product/addProduct',
                data: {
                    token: app.globalData.token,
                    name: that.data.productName,
                    price: that.data.price,
                    file: that.data.filesId[0],
                    on_sale: 1
                },
                // method:'GET',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    wx.hideLoading();
                    if (app.requestEro(res.data.success, res.data.msg)) {
                        wx.showModal({
                            title: '添加成功',
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }
                            }
                        });
                    }
                },
                fail: function (err) {
                    console.log(err)
                }
            })
        }
    }, 1000),

})